package com.example.jason.helloworld;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.Objects;

public class CallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        System.out.println("Action" + intent.getAction());
        //如果是去电
        if (Objects.equals(intent.getAction(), Intent.ACTION_NEW_OUTGOING_CALL)) {
            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.d(">>>>>>>>>>", "call OUT:" + phoneNumber);
            Bundle bdl = new Bundle();
            bdl.putString("type", "call");
            Message msg = new Message();
            msg.setData(bdl);
            bdl.putString("call", "Call out:" + phoneNumber);
            MainActivity.mMyHandle.sendMessage(msg);
        } else {
// 查了下android文档，貌似没有专门用于接收来电的action,所以，非去电即来电.
// 如果我们想要监听电话的拨打状况，需要这么几步 :
// * 第一：获取电话服务管理器TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE);
// * 第二：通过TelephonyManager注册我们要监听的电话状态改变事件。
// * manager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
// * 这里的PhoneStateListener.LISTEN_CALL_STATE就是我们想要监听的状态改变事件，初次之外，还有很多其他事件哦。
// * 第三：通过extends PhoneStateListener来定制自己的规则。将其对象传递给第二步作为参数。
// * 第四：这一步很重要，那就是给应用添加权限。android.permission.READ_PHONE_STATE
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
            if (tm != null) {
                tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
            } else {
                // TODO: Nothing to do
            }
            // 设置一个监听器
        }
    }

    PhoneStateListener listener = new PhoneStateListener() {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (MainActivity.mCallListenEnabled) {
                //注意，方法必须写在super方法后面，否则incomingNumber无法获取到值。
                super.onCallStateChanged(state, incomingNumber);
                switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:
//                    System.out.println("挂断");
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
//                    System.out.println("接听");
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        System.out.println("响铃: 来电号码" + incomingNumber);
                        // 输出来电号码
                        Bundle bdl = new Bundle();
                        bdl.putString("type", "call");
                        bdl.putString("call", "Call in:" + incomingNumber);
                        Message msg = new Message();
                        msg.setData(bdl);
                        MainActivity.mMyHandle.sendMessage(msg);
                        break;
                    default:
                        break;
                }
            } else {
                // TODO: Nothing to do
            }
        }
    };
}


